/**
 * 
 */
package service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import model.Film;
import model.Film.FilmInfo;
import service.DatentraegerService;
import service.FilmService;
import service.util.ServiceAnnotation;
import service.util.ServiceException;
import service.util.ServiceFactory;
import controller.util.JDBCConnectionController;
import dao.DAO;
import dao.FilmSqlDAO;
import dao.SqlDao;
import dao.util.DaoContextEnum;


@SuppressWarnings("serial")
@ServiceAnnotation(serviceName = "FilmService", daos = {"FilmSqlDAO"})
public class FilmServiceImpl implements FilmService
{
	private DAO<Film> dao;
	
	/* (non-Javadoc)
	 * @see at.spengergasse.service.FilmService#deleteFilm(at.spengergasse.model.Film)
	 */
	
	public int deleteFilm(Film Film)
	{
		try
		{
			Connection conn = JDBCConnectionController.getConnection();
			if(((FilmSqlDAO)dao).delete(conn, Film)==1)
			{
				conn.commit();
				return 1;
			}
			conn.rollback();
			return 0;
		} catch (SQLException e)
		{
			throw new ServiceException(e.getMessage(), e);
		}
	}

	/* (non-Javadoc)
	 * @see at.spengergasse.service.FilmService#getDao()
	 */
	
	public DAO<Film> getDao()
	{
		return dao;
	}

	

	/* (non-Javadoc)
	 * @see at.spengergasse.service.FilmService#getFilmById(java.lang.String)
	 */
	
	public Film getFilmById(String id)
	{
		try
		{
			if(id==null)
			{
				throw new ServiceException("id must not be null!");
			}
				else 
			{
				try
				{
					long lid = Long.parseLong(id);
					if(lid>=0l){
						return ((FilmSqlDAO)dao).findById(JDBCConnectionController.getConnection(), lid);
					}
				}
					catch (NumberFormatException e)
					{
						throw new ServiceException(e.getMessage());
				}
			}
			return null;
		} 
			catch (SQLException e)
		{
			throw new ServiceException(e.getMessage(), e);
		}
	}

	/* (non-Javadoc)
	 * @see service.FilmService#getFilme(model.Datentraeger)
	 */
	
	public List<Film> getFilme(String datentraegerId)
	{
		try
		{
			if(datentraegerId==null)
			{
				return ((FilmSqlDAO)dao).findAll(JDBCConnectionController.getConnection());
			}
				else 
				{
					Long datId = Long.parseLong(datentraegerId);
					return ((FilmSqlDAO)dao).findAllFromDatentraeger(JDBCConnectionController.getConnection(), datId);
				}
		} catch (SQLException e)
		{
			throw new ServiceException(e.getMessage(), e);
		}
	}

	/* (non-Javadoc)
	 * @see service.FilmService#saveToDatabase(model.Film)
	 */
	
	public Film saveToDatabase(Film Film)
	{
		try
		{
			Connection conn = JDBCConnectionController.getConnection();
			if(Film == null){
				throw new ServiceException("Film must not be null!");
			}
			
			Film = ((SqlDao<Film>) dao).save(conn, Film);
			if(!Film.isNew()){
				conn.commit();
				return Film;
			}
			conn.rollback();
			return null;
		} catch (SQLException e)
		{
			throw new ServiceException(e.getMessage(), e);
		}
	}
	
	public ArrayList<FilmInfo> getFilmInfos(String DatId)
	{
		Film actFilm;
		List<Film>allFilm=getFilme(DatId);
		ArrayList<FilmInfo>infos=new ArrayList<FilmInfo>();
		Iterator<Film>ite=allFilm.iterator();
		while(ite.hasNext()){
			actFilm = ite.next();
			if(actFilm.getDatentraegerID()!=null && actFilm.getDatentraegerID()!=0){
				actFilm.getInfo().setFilmDat(((DatentraegerService) ServiceFactory.getServiceForName("DatentraegerService", DaoContextEnum.MYSQL)).getDatentraegerById(""+actFilm.getDatentraegerID()).getName());
			}
			infos.add(actFilm.getInfo());
		}
		return infos;
	}

	/* (non-Javadoc)
	 * @see service.FilmService#setFilmSqlDao(dao.DAO)
	 */
	
	public void setFilmSqlDAO(DAO<Film> dao)
	{
		this.dao=dao;
	}



}
